<template>
  <div class="login">
    <div class="wrap-login100 p-l-55 p-r-55 p-t-65 p-b-54">
                <el-form :model="state" class="login100-form validate-form" v-enter-next-focus
                         @submit.native.prevent="onSubmit" ref="form">
                    <div class="logo_img">
                        <img src="../assets/logo.png" alt="">
                    </div>
                    <span class="login100-form-title">兴中能源</span>
                    <!-- <span v-if="state.名称" style="display: block;text-align: center;font-size: 0.25rem;">{{ state.名称 }}</span> -->
                    <div class="wrap-input100 validate-input m-b-23">
                        <p class="label-input100">用户名</p>
                        <el-form-item prop="username" :rules="[{
                            required: true, message: '请输入用户名', trigger: 'blur'
                        }]">
                            <el-input v-model="state.username" width="100%" @input="debouncedOnChange">
                                <template slot="prefix">
                                    <i class="el-input__icon el-icon-user" style="font-size: 24px;"></i>
                                </template>
                            </el-input>
                            <!-- <template v-slot:error="{error}">
                                <span class="error-msg row"><i class="el-icon-warning mr4"></i>{{ error }}</span>
                            </template> -->
                        </el-form-item>
                    </div>

                    <div class="wrap-input100 validate-input">
                        <p class="label-input100">密码</p>
                        <el-form-item prop="password" :rules="[{
                            required: true, message: '请输入密码', trigger: 'blur'
                        }]">
                            <el-input v-model="state.password" type="password" width="100%">
                                <template slot="prefix">
                                    <i class="el-input__icon el-icon-lock" style="font-size: 24px;"></i>
                                </template>
                            </el-input>
                            <!-- <template v-slot:error="{error}">
                                <span class="error-msg row"><i class="el-icon-warning mr4"></i>{{ error }}</span>
                            </template> -->
                        </el-form-item>
                    </div>

                    <div class="container-login100-form-btn">
                        <div class="wrap-login100-form-btn">
                            <div class="login100-form-bgbtn"></div>
                            <button class="login100-form-btn" type="submit">
                                <i class="el-icon-loading mr4"></i>登 录
                            </button>
                        </div>
                    </div>
                </el-form>
            </div>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import keyInput from './keyInput.vue';
import http from '../api/http'
import { ElMessage } from 'element-plus'
import md5 from 'crypto-js/md5';
import { useRouter } from 'vue-router'
import { debounce, throttle } from 'lodash'

const router = useRouter()
const state = ref({
  username: '',
  password: ''
})
async function onSubmit() {
  if (!state.value.username || !state.value.password) {
    return
  }
  const params = {
    username: state.value.username,
    password: md5((state.value.username + state.value.password)+`20110113`).toString().toUpperCase(),
  }
  http.post('/login', null, {
    params: params,
}).then(res => {
    if (res.data.status === 0) {
      console.log(res.data.data)
      window.sessionStorage.setItem('user', res.data.data)
      ElMessage.success('登录成功')
      router.push('/about')
    } else {
      ElMessage.error(res.data.msg)
    }
  }).catch(error => {
    console.log(error)
  })
}

function onChange() {
  console.log(state.value.username)
}
// let debouncedOnChange = debounce(onChange, 30000)
let debouncedOnChange = throttle(onChange, 500)
onMounted(()=>{
    // debouncedOnChange = debouncedOnChange
})
components: {
  keyInput
}

</script>

<style scoped>
  .login{
    width: 100%;
    min-height: 100vh;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    padding: 15px;
    background-repeat: no-repeat;
    background-position: center center;
    background-size: cover;
    background: url("../assets/bg-01.jpg") no-repeat center;
    background-size: cover;
  }

  .logo {
    display: block;
    margin: -20px auto 20px;
    width: 20%;
}

.p-t-65 {
    padding-top: 65px;
}

.p-b-49 {
    padding-bottom: 40px;
}

.p-b-54 {
    padding-bottom: 54px;
}

.p-l-55 {
    padding-left: 55px;
}

.p-r-55 {
    padding-right: 55px;
}

.m-b-23 {
    margin-bottom: 23px;
}

input {
    outline: none;
    border: none;
}


input:focus::-webkit-input-placeholder {
    color: transparent;
}

input::-webkit-input-placeholder {
    color: rgb(173, 173, 173);
}


button {
    border: none;
    background: transparent;
    outline: none !important;
}

button:hover {
    cursor: pointer;
}


.limiter {
    width: 100%;
    margin: 0px auto;
}

.wrap-login100 {
    width: 500px;
    background: rgba(255, 255, 255, 1);
    border-radius: 10px;
    /*overflow: hidden;*/
}

.login100-form {
    width: 100%;
}

.login100-form-title {
    display: block;
    font-family: "Microsoft Yahei";
    font-size: 37px;
    color: rgb(51, 51, 51);
    line-height: 1.2;
    text-align: center;
    margin-bottom: 20px;
    margin-top: 15px;
}

.wrap-input100 {
    width: 100%;
    position: relative;
    /* border-bottom: 2px solid rgb(217, 217, 217); */
    font-size: 16px;
}

.label-input100 {
    font-family: "Microsoft Yahei";
    font-size: 14px;
    color: rgb(51, 51, 51);
    line-height: 1.5;
    padding-left: 7px;
    margin-bottom: 10px;
}

.input100 {
    font-family: "Microsoft Yahei";
    font-size: 16px;
    color: rgb(51, 51, 51);
    line-height: 1.2;
    display: block;
    width: 100%;
    height: 55px;
    background: transparent;
    padding: 0px 43px 0px 43px;
}

.focus-input100 {
    position: absolute;
    display: block;
    width: 100%;
    height: 100%;
    top: 0px;
    left: 0px;
    pointer-events: none;
}


.focus-input100 i {
    color: rgb(173, 173, 173);
    font-size: 22px;
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    height: 100%;
    bottom: 0px;
    left: 0px;
    padding-left: 13px;
    padding-top: 3px;
}

.focus-input100::before {
    content: "";
    display: block;
    position: absolute;
    bottom: -2px;
    left: 0px;
    width: 0px;
    height: 2px;
    background: rgb(127, 127, 127);
    transition: all 0.4s ease 0s;
}

.input100:focus + .focus-input100::before {
    width: 100%;
}

.has-val.input100 + .focus-input100::before {
    width: 100%;
}

.input100:focus + .focus-input100 i {
    color: rgb(166, 75, 244);
}

.has-val.input100 + .focus-input100 i {
    color: rgb(166, 75, 244);
}

.container-login100-form-btn {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    margin-top: 50px;
}

.wrap-login100-form-btn {
    width: 100%;
    display: block;
    position: relative;
    z-index: 1;
    border-radius: 25px;
    overflow: hidden;
    margin: 0px auto;
    box-shadow: rgba(3, 216, 222, 0.2) 0px 5px 30px 0px;
}

.login100-form-bgbtn {
    position: absolute;
    z-index: -1;
    width: 300%;
    height: 100%;
    background: -webkit-linear-gradient(right, rgb(0, 219, 222), rgb(252, 0, 255), rgb(0, 219, 222), rgb(252, 0, 255));
    top: 0px;
    left: -100%;
    transition: all 0.4s ease 0s;
}

.login100-form-btn {
    font-family: "Microsoft Yahei";
    font-size: 16px;
    color: rgb(255, 255, 255);
    line-height: 1.2;
    text-transform: uppercase;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0px 20px;
    width: 100%;
    height: 50px;
}

.wrap-login100-form-btn:hover .login100-form-bgbtn {
    left: 0px;
}

.validate-input {
    position: relative;
}

.limiter::v-deep {
    .el-form-item {
        margin-bottom: 0;
    }

    .el-form-item__content {
        display: flex;
    }
}

.error-msg {
    position: absolute;
    right: -30px;
    top: 0;
    bottom: 0;
    z-index: 0;
    color: red;
    width: 110px;
}

.active-icon {
    color: rgb(166, 75, 244);
}

.wrap-input100 .input100 {
    padding-right: 100px;
}
.el-input{
  height: 50px !important;
}
.logo_img{
    text-align: center;
    img{
        width: 70px;
        height: 70px;
    }
}
</style>
